{% extends 'base.html' %}

{% block title %}{% if department %}编辑部门{% else %}新增部门{% endif %} - 固定资产管理系统{% endblock %}

{% block content %}
<div class="row">
    <div class="col-md-6">
        <h2>{% if department %}编辑部门{% else %}新增部门{% endif %}</h2>
        
        <form method="post">
            {% csrf_token %}
            
            <div class="mb-3">
                <label for="name" class="form-label">部门名称 *</label>
                <input type="text" class="form-control" id="name" name="name" value="{% if department %}{{ department.name }}{% else %}{{ name|default:'' }}{% endif %}" required>
            </div>
            
            <div class="mb-3">
                <label for="code" class="form-label">部门代码 *</label>
                <input type="text" class="form-control" id="code" name="code" value="{% if department %}{{ department.code }}{% else %}{{ code|default:'' }}{% endif %}" required>
            </div>
            
            <div class="mb-3">
                <label for="organization" class="form-label">所属组织 *</label>
                <select class="form-select" id="organization" name="organization" required>
                    <option value="">请选择组织</option>
                    {% for org in organizations %}
                        <option value="{{ org.id }}" {% if department and department.organization_id == org.id or organization_id|add:"0" == org.id %}selected{% endif %}>
                            {{ org.name }}
                        </option>
                    {% endfor %}
                </select>
            </div>
            
            <div class="mb-3">
                <label for="location" class="form-label">位置</label>
                <select class="form-select" id="location" name="location">
                    <option value="">请选择位置</option>
                    {% for loc in locations %}
                        <option value="{{ loc.id }}" {% if department and department.location_id == loc.id or location_id|add:"0" == loc.id %}selected{% endif %}>
                            {{ loc.name }}
                        </option>
                    {% endfor %}
                </select>
            </div>
            
            <div class="mb-3">
                <label for="parent" class="form-label">上级部门</label>
                <select class="form-select" id="parent" name="parent">
                    <option value="">无上级部门</option>
                    {% for dept in departments %}
                        <option value="{{ dept.id }}" {% if department and department.parent_id == dept.id or parent_id|add:"0" == dept.id %}selected{% endif %}>
                            {{ dept.name }}
                        </option>
                    {% endfor %}
                </select>
            </div>
            
            <div class="mb-3">
                <button type="submit" class="btn btn-primary">{% if department %}更新{% else %}创建{% endif %}</button>
                <a href="{% url 'assets:department_list' %}" class="btn btn-secondary">取消</a>
            </div>
        </form>
    </div>
</div>
{% endblock %}